package tools.crawler.cncn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.junit.Test;

import com.easywan.util.ImageUtils;
import com.easywan.util.StringsUtils;
import com.easywan.util.Utils;

import tools.crawler.utils.HttpRequest;

public class ShenzhenCncnCom {
	@Test
	public void start(){
		String url = "http://shenzhen.cncn.com/jingdian/";
		
		//获取所有文章页
		String html =  HttpRequest.get(url).body();
		//获得文章连接页面的区域
		String areaStart ,areaEnd;
		areaStart = "<div class=\"city_spots_list\">";
		areaEnd = "<div class=\"page\"> ";
		String htmlContent = StringsUtils.findMid(html, areaStart, areaEnd);
		String regex = "<a href=\"(.+?)\"";
		String not = "piao";
		List<String> urls = StringsUtils.mathesAndNot(htmlContent, regex,not);
		//System.out.println(urls);
		for(String urlTemp:urls){
			//System.out.println(urlTemp);
		}
		//采集分页
		String pageUrlTemp = "http://shenzhen.cncn.com/jingdian/1-[1]-0-0.html";
		int start = 2,end = 13;
		int loop = 1;
		String gz = "[1]";
		List<String> pageUrls = StringsUtils.urlToStrings(pageUrlTemp,start,end,loop,gz);
		for(String pageUrl:pageUrls){
			urls.addAll(listPage(url));
		}
		//System.out.println(urls.size());
		
		//采集文章
		for(int i = 0; i < urls.size();i++){
			String contentUrl = urls.get(i)+"profile";
			String contentHtml = HttpRequest.get(contentUrl).body();
			//System.out.println(contentHtml);
			//String contentContent = "";
			
			//字段
			String title = "";
			String titleStart = "<h1>";
			String titleEnd = "</h1>";
			title = StringsUtils.findMid(contentHtml,titleStart, titleEnd);
					
			String from = contentUrl;
			/*String fromStart = "<div class=\"showinfo\"> ";
			String fromEnd = "&nbsp; &nbsp;<span class=\"source\">欣欣旅游网</span> </div>";
			*/
			String content = "";
			String contentStart = "<div class=\"type\"> <div class=\"top\">";
			String contentEnd = "</div></div></div><div class=\"sideR\">";
			content = StringsUtils.findMid(contentHtml,contentStart, contentEnd);
					
			
			/*System.out.println("url:"+contentUrl);
			System.out.println("title:"+title);
			System.out.println("from:"+from);
			System.out.println("content:"+content);*/
			try {
				insert(title,content,from);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	List<String> listPage(String url){
		//获取所有文章页
				String html =  HttpRequest.get(url).body();
				//获得文章连接页面的区域
				String areaStart ,areaEnd;
				areaStart = "<div class=\"city_spots_list\">";
				areaEnd = "<div class=\"page\"> ";
				String htmlContent = StringsUtils.findMid(html, areaStart, areaEnd);
				String regex = "<a href=\"(.+?)\"";
				String not = "piao";
				List<String> urls = StringsUtils.mathesAndNot(htmlContent, regex,not);
				//System.out.println(urls);
				for(String urlTemp:urls){
					//System.out.println(urlTemp);
					
				}
				return urls;
	}
	static Connection conn;
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/easywan","root","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void insert(String s1,String s2,String s3) throws Exception{
		
		PreparedStatement pstmt = conn.prepareStatement("insert into cj_spots_bief(title,content,source_article,createtime) values(?,?,?,?)");
		pstmt.setString(1,s1);
		pstmt.setString(2,s2);
		pstmt.setString(3,s3);
		pstmt.setDate(4, new java.sql.Date(Utils.getNowDate().getTime()));
		int i = pstmt.executeUpdate();
		System.out.println(i);
		pstmt.close();
	}
}
